/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { GeometryTransitionView } from '@hadss/react_native_geometry_transition';
import React, { useState } from 'react';
import { View, Text, TextInput, StyleSheet, ScrollView, TouchableOpacity, Alert } from 'react-native';
import { Intents } from '@hadss/react_native_intents';
import {
  shareMusicIntent,
  shareVideoIntent,
  musicDeleteIntent,
  musicDeleteEntity,
  videoDeleteIntent,
  videoDeleteEntity,
} from './data';

const handleShareIntentPlayMusic = async (data: any): Promise<void> => {
  await Intents.shareIntent([data]).then(() => {
    Alert.alert(`意图分享成功`);
  });
};
const handleDeleteIntent = async (id: any): Promise<void> => {
  try {
    await Intents.deleteIntent(id);
    Alert.alert(`洞察意图删除成功`);
  } catch (error) {
    Alert.alert(`洞察意图删除失败,${JSON.stringify(error)}`);
  }
};
const handleDeleteEntity = async (id: any): Promise<void> => {
  try {
    await Intents.deleteEntity(id, ['C10194368']);
    Alert.alert(`意图实体删除成功`);
  } catch (error) {
    Alert.alert(`意图实体删除失败,${JSON.stringify(error)}`);
  }
};

const IntentFrameworkScreen = (): JSX.Element => {
  const [activeIndex, setActiveIndex] = useState(0);
  const [messages, setMessages] = useState<string>('请先点击“获取XXX数据”或输入数据，再点击“功能按钮”');
  const [shareData, setShareData] = useState<any>(shareMusicIntent);
  const [deletelntent, setDeletelntent] = useState<string>('PlayMusic');
  const [deleteEntity, setDeleteEntity] = useState<string>('Music');
  return (
    <View style={styles.container}>
      <View style={{ flexDirection: 'row', justifyContent: 'flex-start', width: '100%', marginBottom: 16 }}>
        <Text style={{ fontSize: 30 }}>Lnsightintent Test</Text>
      </View>

      <TextInput multiline={true} style={styles.textinput}>
        {messages}
      </TextInput>

      <ScrollView style={{ width: '100%' }} showsVerticalScrollIndicator={false}>
        <TouchableOpacity
          style={[{ backgroundColor: activeIndex === 1 ? 'rgb(10, 89, 247)' : 'rgba(0, 0, 0, 0.05)' }, styles.button]}
          onPress={(): void => {
            setMessages(JSON.stringify(shareMusicIntent));
            setActiveIndex(1);
            setShareData(shareMusicIntent);
          }}>
          <Text style={[{ color: activeIndex === 1 ? 'white' : 'rgb(10, 89, 247)' }, styles.btnText]}>
            获取 音乐insightlntents
          </Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={[{ backgroundColor: activeIndex === 2 ? 'rgb(10, 89, 247)' : 'rgba(0, 0, 0, 0.05)' }, styles.button]}
          onPress={(): void => {
            setMessages(JSON.stringify(shareVideoIntent));
            setActiveIndex(2);
            setShareData(shareVideoIntent);
          }}>
          <Text style={[{ color: activeIndex === 2 ? 'white' : 'rgb(10, 89, 247)' }, styles.btnText]}>
            获取 视频insightlntents
          </Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={[{ backgroundColor: activeIndex === 3 ? 'rgb(10, 89, 247)' : 'rgba(0, 0, 0, 0.05)' }, styles.button]}
          onPress={(): void => {
            setMessages(JSON.stringify(musicDeleteIntent));
            setActiveIndex(3);
            setDeletelntent('PlayMusic');
          }}>
          <Text style={[{ color: activeIndex === 3 ? 'white' : 'rgb(10, 89, 247)' }, styles.btnText]}>
            获取 音乐deletelntent
          </Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={[{ backgroundColor: activeIndex === 4 ? 'rgb(10, 89, 247)' : 'rgba(0, 0, 0, 0.05)' }, styles.button]}
          onPress={(): void => {
            setMessages(JSON.stringify(videoDeleteIntent));
            setActiveIndex(4);
            setDeletelntent('PlayVideo');
          }}>
          <Text style={[{ color: activeIndex === 4 ? 'white' : 'rgb(10, 89, 247)' }, styles.btnText]}>
            获取 视频deletelntent
          </Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={[{ backgroundColor: activeIndex === 5 ? 'rgb(10, 89, 247)' : 'rgba(0, 0, 0, 0.05)' }, styles.button]}
          onPress={(): void => {
            setMessages(JSON.stringify(musicDeleteEntity));
            setActiveIndex(5);
            setDeleteEntity('Music');
          }}>
          <Text style={[{ color: activeIndex === 5 ? 'white' : 'rgb(10, 89, 247)' }, styles.btnText]}>
            获取 音乐deleteEntity
          </Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={[{ backgroundColor: activeIndex === 6 ? 'rgb(10, 89, 247)' : 'rgba(0, 0, 0, 0.05)' }, styles.button]}
          onPress={(): void => {
            setMessages(JSON.stringify(videoDeleteEntity));
            setActiveIndex(6);
            setDeleteEntity('Video');
          }}>
          <Text style={[{ color: activeIndex === 6 ? 'white' : 'rgb(10, 89, 247)' }, styles.btnText]}>
            获取 视频deleteEntity
          </Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={[{ backgroundColor: activeIndex === 7 ? 'rgb(10, 89, 247)' : 'rgba(0, 0, 0, 0.05)' }, styles.button]}
          onPress={async () => {
            const res = await Intents.getSid(false);
            console.log(res, 'res');
            setMessages(res);
            setActiveIndex(7);
          }}>
          <Text style={[{ color: activeIndex === 7 ? 'white' : 'rgb(10, 89, 247)' }, styles.btnText]}>getSid</Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={[{ backgroundColor: activeIndex === 8 ? 'rgb(10, 89, 247)' : 'rgba(0, 0, 0, 0.05)' }, styles.button]}
          onPress={(): void => {
            setActiveIndex(8);
            handleShareIntentPlayMusic(shareData);
          }}>
          <Text style={[{ color: activeIndex === 8 ? 'white' : 'rgb(10, 89, 247)' }, styles.btnText]}>sharelntent</Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={[{ backgroundColor: activeIndex === 9 ? 'rgb(10, 89, 247)' : 'rgba(0, 0, 0, 0.05)' }, styles.button]}
          onPress={(): void => {
            setActiveIndex(9);
            handleDeleteIntent(deletelntent);
          }}>
          <Text style={[{ color: activeIndex === 9 ? 'white' : 'rgb(10, 89, 247)' }, styles.btnText]}>
            deleteIntent
          </Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={[{ backgroundColor: activeIndex === 10 ? 'rgb(10, 89, 247)' : 'rgba(0, 0, 0, 0.05)' }, styles.button]}
          onPress={(): void => {
            setActiveIndex(10);
            handleDeleteEntity(deleteEntity);
          }}>
          <Text style={[{ color: activeIndex === 10 ? 'white' : 'rgb(10, 89, 247)' }, styles.btnText]}>
            deleteEntity
          </Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={[{ backgroundColor: activeIndex === 11 ? 'rgb(10, 89, 247)' : 'rgba(0, 0, 0, 0.05)' }, styles.button]}
          onPress={async () => {
            const res = await Intents.getSid(true);
            console.log(res, 'res');
            setMessages(res);
            setActiveIndex(11);
          }}>
          <Text style={[{ color: activeIndex === 11 ? 'white' : 'rgb(10, 89, 247)' }, styles.btnText]}>
            getSidRenew
          </Text>
        </TouchableOpacity>
      </ScrollView>
    </View>
  );
};

export default IntentFrameworkScreen;

const styles = StyleSheet.create({
  container: {
    backgroundColor: '#F1F3F5',
    paddingHorizontal: 16,
    paddingTop: 72,
    justifyContent: 'flex-start',
    alignItems: 'center',
    width: '100%',
    height: '100%',
  },
  geometryTransitionView: {
    marginVertical: 10,
    height: 200,
    width: 300,
  },
  textinput: {
    backgroundColor: 'white',
    width: '100%',
    minHeight: 120,
    marginBottom: 48,
    borderRadius: 16,
    paddingHorizontal: 16,
    paddingVertical: 8,
    color: 'rgba(0, 0, 0, 0.6)',
  },
  button: {
    width: '100%',
    height: 40,
    borderRadius: 20,
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
    marginBottom: 12,
  },
  btnText: {
    fontSize: 16,
    fontWeight: '500',
  },
});
